$(function () {
    //页面加载完成获取第一页数据
    getIntegralRecordList(1);
    $('body').on('click','#btn1,#btn2',function () {
        getIntegralRecordList(1)
    });
    //validateData();
    $('body').on('click','#onBack',function (){
        onBackHistory();
    });
    $('body').on('click','#submitUserIntegralRule',function (){
        saveUserIntegralRule();
    });
    $('body').on('change', "#recordType", function () {
        changeIntegralRule();
    });
    //inputRadioSel($(".i-checks"),selectCall);

    //表格中单个字段的修改
    $('body').on('click', '.resetdBtn', function() {
        var  score  =$(this).data('score');
        var  type = $(this).data('type');
        var   id = $(this).data('id');
        parent.layer.open({
            type: 1,
            area: ['380px'],
            skin: 'layui-layer-demo',
            title: '修改此项',
            content: '<div class="m-t m-r m-l m-b"><input class="form-control" value="'+score+'" id="userIntegralScore" name="userIntegralRule.score" type="text"></div>',
            btn: '确定',
            yes: function(index, layero) {
                var userIntegralScore = layero.find('#userIntegralScore').val();
                $.ajax({
                    url:baseloaction+"/admin/user/cus/saveUserIntegralRuleScore",
                    type:"post",
                    data:{"id":id,"score":userIntegralScore},
                    dataType:"json",
                    success:function (result) {
                        parent.layer.close(index);
                        if (result.success == true) {
                            icModale(0,result.message);
                            if(type==1||type==3){
                                $('.userIntegralScore_'+id).html(userIntegralScore+"分数");
                            }
                            if(type==2){
                                $('.userIntegralScore_'+id).html(userIntegralScore+"%");
                            }
                            $('.userIntegralScore_'+id).append('<span class="resetdBtn fs16 c-red" data-id="'+id+'" data-type="'+type+'"  data-score="'+userIntegralScore+'" title="修改"><i class="fa fa-pencil"></i></span>');
                        } else {
                            icModale(0,result.message);
                        }
                    }
                });
            }
        });
    });
});
/**
 * 取消
 */
function onBackHistory() {
    window.history.go(-1);
}
/**
 * 选择回调
 */
function selectCall(em) {
    if(em.name=='type'){
        getProportionIntegral(em.value);
    }
}


/**
 * 提交方法
 */
function saveUserIntegralRule() {
    var params = $("#userIntegralFrom").serializeJSON();
    params = JSON.stringify(params);
    $.ajax({
        url:baseloaction+"/admin/user/cus/saveUserIntegralRule",
        data:params,
        type:"post",
        dataType:"json",
        contentType:'application/json;charset=utf-8',
        success:function (result) {
            if (result.success == true) {
                icModale(1,result.message,1,function () {
                    window.location.href=baseloaction+"/admin/user/cus/toCusUserIntegralRuleListPage";
                });
            } else {
                icModale(0,result.message);
            }
        }
    });
}


/**
 * 获取积分流水列表页面
 * @param pageNo 当前页码
 */
/*function getIntegralRecordList(pageNo) {
    var params = '{}';
    var obj = {
        url: baseloaction + '/admin/user/cus/ajax/queryIntegralRecord',//请求URL
        contextId:'integral_record_list_content',//响应内容页面设置内容ID
        params:params,//请求参数
        pageNo:pageNo,//页码
        pageCall:getIntegralRecordList(1), //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        // selectAllId:'select-all-role', //全选框ID
    };
    ajaxGetListData(obj)
}*/


function getIntegralRecordList(pageNo) {
    debugger
    var params = $("#search-form").serializeJSON();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/user/cus/ajax/queryIntegralRecord',//请求URL
        contextId: 'integral_record_list_content',//响应内容页面设置内容ID
        params: params,//请求参数
        pageNo: pageNo,//页码
        pageCall: getIntegralRecordList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        //selectAllId: 'all-shopTenant', //全选框ID
        callback: function () {//数据加载完成回调
            return true
        }
    };
    ajaxGetListData(obj);
}


function changeIntegralRule() {
    var type = $("#recordType").val();
    var getType=
        "<option value=''>积分活动</option>"+
        "<option value='REGISTER'>学员注册</option>" +
        "<option value='LOGIN'>学员登录</option>" +
        "<option value='PAYSUCCESS'>支付成功</option>" +
        "<option value='GIFT'>后台赠送</option>" +
        "<option value='COURSE'>观看课程</option>" +
        "<option value='EXERCISE'>考试答题</option>" +
        "<option value='QUESTION'>学员提问</option>" +
        "<option value='ONLOOK'>学员围观</option>" +
        "<option value='ANSWER'>回答问题</option>" +
        "<option value='TOPIC'>发表话题</option>" +
        "<option value='SHARE'>学员分享</option>" +
        "<option value='SIGNIN'>学员签到</option>";
    var setType=
        "<option value=''>积分活动</option>"+
        "<option value='GOODS'>兑换商品</option>" +
        "<option value='MONEY'>兑换现金</option>";

    if (type=='2') {
        $("#ruleKey").html(getType);
    }else if(type=='1'){
        $("#ruleKey").html(setType)
    }else {
        $("#ruleKey").html("<option value=''>积分活动</option>");
    }
}
